package com.apache.security.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class XssUtil {

	private static Logger log = Logger.getLogger(XssUtil.class);
	private static String xssType = "<script[^>]*?>.*?</script>";

	private static Pattern xssPattern = Pattern.compile(xssType);

	public static String xssFilter(String input, String filterType) {
		log.info("request param=" + input);
		if (input == null || input.isEmpty()) {
			return input;
		}
		if (filterType == null || !XssFilterTypeEnum.checkValid(filterType)) {
			filterType = XssFilterTypeEnum.ESCAPSE.getValue();
		}
		if (filterType.equals(XssFilterTypeEnum.ESCAPSE.getValue())) {
			Matcher matcher = xssPattern.matcher(input);
			if (matcher.find()) {
				return matcher.group().replace("<", "&lt;").replace(">", "&gt;");
			}
		}
		if (filterType.equals(XssFilterTypeEnum.DELETE.getValue())) {
			return input.replaceAll(xssType, "");
		}
		return input;
	}

	public static enum XssFilterTypeEnum {
		ESCAPSE("escapse"), NO("no"), DELETE("delete");

		private String value;

		private XssFilterTypeEnum(String type) {
			this.value = type;
		}

		public String getValue() {
			return this.value;
		}

		public static boolean checkValid(String type) {
			if (type == null) {
				return false;
			}
			return (ESCAPSE.getValue().equals(type) || NO.getValue().equals(type) || DELETE.getValue().equals(type));
		}
	}

	public static boolean isNull(String text) {
		if (null == text || "".equals(text.trim()) || "null".equalsIgnoreCase(text.trim()))
			return true;
		return false;
	}

	public static String doNull(String val, String defaultVal) {
		return isNull(val) ? defaultVal : val;
	}

	public static boolean isNotNull(String str) {
		return !isNull(str);
	}

}
